import { messages } from "share/common";
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {
  //获取值列表
  getSystemValueList(code) {
    let url = '';
    if (Number(code) > 2000) {
      url = '/api/custom/enumerations/template/by/type?enabled=true&type=';
    } else {
      url = '/api/custom/enumeration/system/by/type?systemCustomEnumerationType=';
    }
    return httpFetch.get(`${config.baseUrl}${url}${code}`)
      .then(res => {
        return new Promise((resolve) => {
          if (res.data.splice) {
            let result = JSON.parse(JSON.stringify(res));
            result.data = { values: res.data };
            resolve(result);
          } else {
            resolve(res);
          }
        });
      });
  },
  //报销单状态
  getValue5002(invoiceStatusList, invoiceStatusListAddVendorList) {
    this.getSystemValueList(5002)
      .then(res => {
        let list = res.data.values;
        invoiceStatusList.length = 0;
        invoiceStatusListAddVendorList.length = 0;
        list.map(item => {
          let data = {};
          data.value = item.value;
          data.label = item.messageKey;
          invoiceStatusList.push(data);
          invoiceStatusListAddVendorList.push(data);
        });
        let data = {};
        data.value = "0000";
        data.label = messages('data-center.key84')/*消费商(公司支付)*/;
        invoiceStatusListAddVendorList.push(data);
      })
  },
  //币种
  getValueCurrency(currencyTypeList) {
    return new Promise((resolve, reject) => {
      currencyTypeList = [];
      resolve([])
    });
    let params = {
      setOfBooksId: '985801821784547329',
      language: 'zh_cn',
      enable: true
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/currency/rate/list/all', params)
        .then((response) => {
          let obj = response.data;
          for (let key in obj) {
            let data = {};
            data.value = obj[key] + '';
            data.label = key;
            currencyTypeList.push(data);
          }
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  getformTypeList(formTypeList, params) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/custom/forms/tenant/all', params)
        .then((response) => {
          let obj = response.data;
          for (let key in obj) {
            let data = {};
            data.value = obj[key] + '';
            data.label = key;
            formTypeList.push(data);
          }
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  //获取热门城市，当做报表查询条件
  getPopularCity(langCode, popularCityList) {
    let params = {
      language: langCode,//"zh_cn",en
      vendorType: "standard"
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/location-service/api/location/popular/city', params)
        .then((response) => {
          let list = response.data;
          popularCityList.length = 0;
          //返回数据样例
          // let arr = [{
          //   "code": "CHN011000000",
          //   "type": "CITY/STATE",
          //   "country": "中国",
          //   "state": "北京",
          //   "city": "北京",
          //   "district": "北京",
          //   "vendorType": "standard"
          // }]
          list.map(item => {
            let data = {};
            // data.value = item.code;
            //搜索城市上传也传名称
            data.value = item.district;
            data.label = item.district;
            popularCityList.push(data);
          });
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  //获取发票标签
  getReceiptLabelTypes(receiptLabelTypesList) {
    let params = {
      listType: "OTHER",
      listCode: "systemLabel-RECEIPT",
      page: 0,
      size: 100,
    };
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/listCode', params)
        .then((response) => {
          let list = response.data;
          list.map(item => {
            let data = {};
            data.value = item.code;
            data.label = item.name;
            receiptLabelTypesList.push(data)
          });
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 获取单据标签
  getDocLabels(targetList) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/expense/labels?labelCategory=1&roleType=TENANT', { page: 0, size: 1000 })
        .then((response) => {
          let list = response.data;
          list.map(item => {
            let data = {};
            data.value = item.type;
            data.label = item.name;
            targetList.push(data)
          });
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 通用获取值列表
  getValueListCommon(code, targetList) {
    this.getSystemValueList(code)
      .then(res => {
        let list = res.data.values;
        targetList.length = 0;
        list.map(item => {
          let data = {};
          data.value = item.value;
          data.label = item.messageKey;
          targetList.push(data)
        })
      })
  }

}
